package com.example.firstjavamqtt.factory.strategy.DeviceLogMessage;

import com.alibaba.fastjson.JSONObject;
import com.example.firstjavamqtt.common.excepiton.GlobalException;
import com.example.firstjavamqtt.common.result.ResultCodeEnum;
import com.example.firstjavamqtt.model.device.TemperatureHumidityMonitor;
import org.springframework.stereotype.Component;

//生成温湿度操作的设备日志策略
@Component
public class TemperatureHumidityMessageStrategy implements DeviceLogMessageStrategy {

    @Override
    public String getOperation(String string) {
        TemperatureHumidityMonitor temperatureHumidityMonitor
                = JSONObject.parseObject(string, TemperatureHumidityMonitor.class);
        if (!temperatureHumidityMonitor.isOnline()) {
            throw new GlobalException(ResultCodeEnum.OFFLINE);
        }
        double humidity = temperatureHumidityMonitor.getHumidity();
        double temperature = temperatureHumidityMonitor.getTemperature();
        return "温度:" + temperature + " 湿度:" + humidity;
    }
}
